package com.cnzhy.esclientrhl.controller;

import com.cnzhy.esclientrhl.index.IndexDemo;
import com.cnzhy.esclientrhl.repository.IndexDemoRepository;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *
 **/
@RestController
public class IndexDemoController {

    private IndexDemoRepository indexDemoRepository;

    /**
     * 使用setter 方法注入
     */
    @Autowired
    public void setIndexDemoRepository(IndexDemoRepository indexDemoRepository) {
        this.indexDemoRepository = indexDemoRepository;
    }

    //测试URL: http://127.0.0.1:8888/demo/add
    @GetMapping("/demo/add")
    public String add() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposalNo("1");
        indexDemo.setAppli_name("新增数据测试");
        indexDemo.setRisk_code("aa1");
        indexDemo.setSum_premium(1);
        indexDemoRepository.save(indexDemo);
        return "新增成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/demo/add_list
    @GetMapping("/demo/add_list")
    public String addList() throws Exception {
        IndexDemo indexDemo2 = new IndexDemo();
        indexDemo2.setProposalNo("2");
        indexDemo2.setAppli_name("新增列表测试1");
        indexDemo2.setRisk_code("新增列表测试1");
        indexDemo2.setSum_premium(2);
        IndexDemo indexDemo3 = new IndexDemo();
        indexDemo3.setProposalNo("3");
        indexDemo3.setAppli_name("新增列表测试2");
        indexDemo3.setRisk_code("新增列表测试2");
        indexDemo3.setSum_premium(3);
        indexDemoRepository.save(Arrays.asList(indexDemo2, indexDemo3));
        return "新增列表测试成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/demo/update
    @GetMapping("/demo/update")
    public String update() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposalNo("1");
        indexDemo.setAppli_name("a999999");
        indexDemo.setRisk_code("aa9999999");
        indexDemo.setSum_premium(99999);
        indexDemoRepository.update(indexDemo);
        return "修改成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/demo/delete
    @GetMapping("/demo/delete")
    public String delete() throws Exception {
        IndexDemo indexDemo = new IndexDemo();
        indexDemo.setProposalNo("1");
//        indexDemo.setAppli_name("a999999");
//        indexDemo.setRisk_code("a9999999");
//        indexDemo.setSum_premium(99999);
        indexDemoRepository.delete(indexDemo);
        return "删除成功：" + new Timestamp(new Date().getTime());
    }

    //测试URL: http://127.0.0.1:8888/demo/query
    @GetMapping("/demo/query")
    public List<IndexDemo> query() throws Exception {
        List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchAllQuery());
        return search;
    }

    @GetMapping("/demo/query2")
    public List<IndexDemo> query2() throws Exception {
        List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchAllQuery());
        return search;
    }

    @GetMapping("/demo/index")
    public List<IndexDemo> searchIndex() throws Exception {
        SearchRequest searchRequest = new SearchRequest("1");
        List<IndexDemo> search = indexDemoRepository.search(QueryBuilders.matchQuery("1", "1"));
        return search;
    }
}
